/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.cras.webcam.http;


import java.nio.ByteBuffer;

public class HttpMessage extends HttpContent{
    private static final int HASH_CODE_PRIME = 31;
    private DecoderResult decoderResult = DecoderResult.SUCCESS;
    private HttpVersion version;
    private final HttpHeaders headers;

    protected HttpMessage(final HttpVersion version,ByteBuffer content) {
        super(content);
        if (version == null) {
            throw new NullPointerException("version");
        }
        this.version = version;
        headers = new HttpHeaders();
    }
    /**
     * Returns the protocol version of this {@link HttpMessage}
     */
    public HttpVersion protocolVersion(){
        return  version;
    }

    public HttpMessage setProtocolVersion(HttpVersion version) {
        if (version == null) {
            throw new NullPointerException("version");
        }
        this.version = version;
        return this;
    }

    public void setDecoderResult(DecoderResult decoderResult) {
        if (decoderResult == null) {
            throw new NullPointerException("decoderResult");
        }
        this.decoderResult = decoderResult;
    }


    /**
     * Returns the headers of this message.
     */
    public HttpHeaders headers(){
        return headers;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = HASH_CODE_PRIME * result + headers.hashCode();
        result = HASH_CODE_PRIME * result + version.hashCode();
        result = HASH_CODE_PRIME * result + super.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof HttpMessage)) {
            return false;
        }

        HttpMessage other = (HttpMessage) o;

        return headers().equals(other.headers()) &&
                protocolVersion().equals(other.protocolVersion()) &&
                super.equals(o);
    }
}
